[20220812_140342_user] D2_main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. #define CLR_DS18B20() HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0,GPIO_PIN_RESET )
  40. #define SET_DS18B20() HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0,GPIO_PIN_SET )
  41. #define DS18B20_DQ_IN HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0)
  42. int16_t temp;
  43. int error;
  44. float M14_DS18B20 =0;
  45. /* USER CODE BEGIN PV */
  46. typedef void (*pFunction)(void);
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. void MX_ADC1_Init1(char pin);
  51. void sensor(void);
  52. uint8_t DS18B20_Init(void);
  53. short DS18B20_Get_Temp(void);
  54. void DS18B20_Start(void);
  55. void DS18B20_Write_Byte(uint8_t dat);
  56. uint8_t DS18B20_Read_Byte(void);
  57. uint8_t DS18B20_Read_Bit(void);
  58. uint8_t DS18B20_Check(void);
  59. void DS18B20_Rst(void);
  60. /* USER CODE BEGIN PFP */
  61. /* USER CODE END PFP */
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64. int _write(int fd, char *ch, int len)
  65. {
  66. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  67. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  68. return len;
  69. }
  70. void print(char* s, float c)
  71. { int w;
  72. int w_d;
  73. int w_f;
  74. if(c<0)
  75. {
  76. w = c*100;
  77. w_d = w/100;
  78. w_f = (-1*w)%100;
  79. }
  80. else
  81. {
  82. w = c*100;
  83. w_d = w/100;
  84. w_f = w%100;
  85. }
  86. printf("%s = %d.%d\n",s,w_d,w_f);
  87. }
  88. /* USER CODE END 0 */
  89. /**
  90. * @brief The application entry point.
  91. * @retval int
  92. */
  93. int main(void)
  94. {
  95. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  96. HAL_Init();
  97. SystemClock_Config();
  98. MX_GPIO_Init();
  99. MX_USART2_UART_Init();
  100. MX_UART4_Init();
  101. MX_USART1_UART_Init();
  102. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  103. HAL_Delay(1000);
  104. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  105. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  106. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  107. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  108. {
  109. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  110. }
  111. else if(zigbeestatu[18]== 0x05)
  112. {
  113. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  114. }
  115. while (1)
  116. {
  117. sensor();
  118. if(M14_DS18B20<16)
  119. {
  120. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  121. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  122. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  123. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  124. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  125. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  126. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  127. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  128. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  129. }
  130. }
  131. }
  132. void SystemClock_Config(void)
  133. {
  134. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  135. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  136. __HAL_RCC_PWR_CLK_ENABLE();
  137. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  138. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  139. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  140. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  141. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  142. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  143. RCC_OscInitStruct.PLL.PLLM = 8;
  144. RCC_OscInitStruct.PLL.PLLN = 72;
  145. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  146. RCC_OscInitStruct.PLL.PLLQ = 3;
  147. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  148. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  149. {
  150. Error_Handler();
  151. }
  152. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  153. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  154. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  155. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  156. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  157. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  158. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. }
  163. void user_pwm_setvalue(uint16_t value)
  164. {
  165. TIM_OC_InitTypeDef sConfigOC;
  166. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  167. sConfigOC.Pulse = value;
  168. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  169. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  170. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  171. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  172. }
  173. void user_pwm_setvalue2(uint16_t value)
  174. {
  175. TIM_OC_InitTypeDef sConfigOC;
  176. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  177. sConfigOC.Pulse = value;
  178. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  179. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  180. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  181. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  182. }
  183. void sensor(void)
  184. {
  185. temp=DS18B20_Get_Temp();
  186. HAL_Delay(10000);
  187. }
  188. void delay_us(uint32_t value)
  189. {
  190. uint32_t i;
  191. i = value * 3;
  192. while(i--);
  193. }
  194. void DS18B20_Rst(void)
  195. {
  196. CLR_DS18B20();
  197. delay_us(750);
  198. SET_DS18B20();
  199. delay_us(15);
  200. }
  201. uint8_t DS18B20_Check(void)
  202. {
  203. uint8_t retry=0;
  204. while (DS18B20_DQ_IN&&retry<200)
  205. {
  206. retry++;
  207. delay_us(1);
  208. }
  209. if(retry>=200)return 1;
  210. else retry=0;
  211. while (!DS18B20_DQ_IN&&retry<240)
  212. {
  213. retry++;
  214. delay_us(1);
  215. }
  216. if(retry>=240)return 1;
  217. return 0;
  218. }
  219. uint8_t DS18B20_Read_Bit(void)
  220. {
  221. uint8_t data;
  222. CLR_DS18B20();
  223. delay_us(2);
  224. SET_DS18B20();
  225. delay_us(12);
  226. if(DS18B20_DQ_IN)data=1;
  227. else data=0;
  228. delay_us(50);
  229. return data;
  230. }
  231. uint8_t DS18B20_Read_Byte(void)
  232. {
  233. uint8_t i,j,dat;
  234. dat=0;
  235. for (i=1;i<=8;i++)
  236. {
  237. j=DS18B20_Read_Bit();
  238. dat=(j<<7)|(dat>>1);
  239. }
  240. return dat;
  241. }
  242. void DS18B20_Write_Byte(uint8_t dat)
  243. {
  244. uint8_t j;
  245. uint8_t testb;
  246. for (j=1;j<=8;j++)
  247. {
  248. testb=dat&0x01;
  249. dat=dat>>1;
  250. if (testb)
  251. {
  252. CLR_DS18B20();
  253. delay_us(2);
  254. SET_DS18B20();
  255. delay_us(60);
  256. }
  257. else
  258. {
  259. CLR_DS18B20();
  260. delay_us(60);
  261. SET_DS18B20();
  262. delay_us(2);
  263. }
  264. }
  265. }
  266. uint8_t DS18B20_Init(void)
  267. {
  268. SET_DS18B20();
  269. DS18B20_Rst();
  270. return DS18B20_Check();
  271. }
  272. void DS18B20_Start(void)
  273. {
  274. DS18B20_Rst();
  275. DS18B20_Check();
  276. DS18B20_Write_Byte(0xcc);
  277. DS18B20_Write_Byte(0x44);
  278. }
  279. short DS18B20_Get_Temp(void)
  280. {
  281. uint8_t temp;
  282. uint8_t TL,TH;
  283. short tem;
  284. DS18B20_Start();
  285. DS18B20_Rst();
  286. DS18B20_Check();
  287. DS18B20_Write_Byte(0xcc);
  288. DS18B20_Write_Byte(0xbe);
  289. TL=DS18B20_Read_Byte();
  290. TH=DS18B20_Read_Byte();
  291. if(TH>7)
  292. {
  293. TH=~TH;
  294. TL=~TL;
  295. temp=0;
  296. }
  297. else temp=1;
  298. tem=TH;
  299. tem<<=8;
  300. tem+=TL;
  301. tem=(float)tem*0.625f;
  302. if(temp)return tem;
  303. else return -tem;
  304. }
  305. void MX_ADC1_Init1(char pin)
  306. {
  307. ADC_ChannelConfTypeDef sConfig = {0};
  308. hadc1.Instance = ADC1;
  309. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  310. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  311. hadc1.Init.ScanConvMode = DISABLE;
  312. hadc1.Init.ContinuousConvMode = DISABLE;
  313. hadc1.Init.DiscontinuousConvMode = DISABLE;
  314. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  315. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  316. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  317. hadc1.Init.NbrOfConversion = 1;
  318. hadc1.Init.DMAContinuousRequests = DISABLE;
  319. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  320. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  321. {
  322. Error_Handler();
  323. }
  324. sConfig.Channel = pin;
  325. sConfig.Rank = 1;
  326. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  327. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  328. {
  329. Error_Handler();
  330. }
  331. }
  332. void MX_GPIO_Input1(unsigned long pin)
  333. {
  334. GPIO_InitTypeDef GPIO_InitStruct = {0};
  335. __HAL_RCC_GPIOE_CLK_ENABLE();
  336. GPIO_InitStruct.Pin = pin;
  337. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  338. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  339. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  340. }
  341. void Error_Handler(void)
  342. {
  343. }